package ru.ssau.customerManagement.controller;

import ru.ssau.customerManagement.controller.remoting.notifications.ServerSideNotificationController;
import ru.ssau.customerManagement.model.Model;
import ru.ssau.customerManagement.model.ModelImpl;
import ru.ssau.customerManagement.model.entities.Customer;
import ru.ssau.customerManagement.model.entities.CustomerLocation;

import java.net.Socket;
import java.util.Collections;
import java.util.List;

/**
 * Author: illabefat
 * Time: 02.11.2014
 */
public class ServerSideControllerImpl implements ServerSideController{
    private Model model = new ModelImpl();
    private ServerSideNotificationController notificationController = new ServerSideNotificationController();

    public ServerSideControllerImpl(Model model) {
        this.model = model;
    }

    @Override
    public void addListener(Socket socket) {
        notificationController.addListener(socket);
    }

    public Customer getCustomer(Long id)
    {
        return model.getCustomer(id);
    }

    public void addCustomer(Customer customer) throws CreationException
    {
        if (customer == null) return;
        if (model.getCustomer(customer.getId()) != null)
        {
            throw new CreationException("Customer with id=" + customer.getId() + " already exists");
        }
        model.addCustomer(customer);

        notificationController.customerAdded(customer);
    }

    public void replaceCustomer(Customer newCustomer)
    {
        if (newCustomer == null) return;
        model.addCustomer(newCustomer);

        notificationController.customerChanged(newCustomer);
    }

    public void deleteCustomer(Long customerId)
    {
        model.removeCustomer(customerId);
        List<CustomerLocation> locations =  model.getCustomerLocationsByCustomer(customerId);
        for (CustomerLocation location : locations)
        {
            model.removeCustomerLocation(location.getId());
        }

        notificationController.customerRemoved(customerId);
    }

    public void addCustomerLocation(CustomerLocation location) throws CreationException
    {
        if (location == null) return;
        if (model.getCustomerLocation(location.getId()) != null)
        {
            throw new CreationException("CustomerLocation with id=" + location.getId() + " already exists");
        }
        model.addCustomerLocation(location);

        notificationController.customerLocationAdded(location);
    }

    public void replaceCustomerLocation(CustomerLocation location)
    {
        model.addCustomerLocation(location);

        notificationController.customerLocationChanged(location);
    }

    public CustomerLocation getLocation(Long id)
    {
        return model.getCustomerLocation(id);
    }

    public void deleteCustomerLocation(Long customerLocationId)
    {
        model.removeCustomerLocation(customerLocationId);

        notificationController.customerLocationRemoved(customerLocationId);
    }

    public List<CustomerLocation> getLocationsByCustomer(Long customerId)
    {
        if (customerId == null)
            return Collections.emptyList();
        return model.getCustomerLocationsByCustomer(customerId);
    }

    public List<Customer> getCustomers()
    {
        return model.getCustomers();
    }

    @Override
    public boolean tryLockCustomer(Long id) {
        return model.lockCustomer(id);
    }

    @Override
    public void unlockCustomer(Long id) {
        model.unlockCustomer(id);
    }

    @Override
    public boolean tryLockCustomerLocation(Long id) {
        return model.lockCustomerLocation(id);
    }

    @Override
    public void unlockCustomerLocation(Long id) {
        model.unlockCustomerLocation(id);
    }


}
